"""
使用非阻塞式进程，进行队列的写入和读取

"""

from multiprocessing import Manager, Pool
from time import sleep


def write(q):
    name = ["a", "b", "I am congyu", 500, "写入结束"]
    for item in name:
        if not q.full():
            print(f"写入进程开始写入：{item}")
            q.put(item)
            sleep(1)
    print("写入进程写入结束")


def reader(q):
    while True:
        if not q.empty():
            text = q.get()
            print(f"读取值： {text}")
            if text == "写入结束":
                break
            sleep(1)
        else:
            print("队列为空，等待写入")
            sleep(2)
            continue


if __name__ == "__main__":
    queue = Manager().Queue()
    pool = Pool(3)
    pool.apply_async(write, (queue,))
    pool.apply_async(reader, (queue,))

    pool.close()
    pool.join()
